﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BuySoftwareOnline.Domain.Concrete;
using BuySoftwareOnline.Domain.Abstract;
using BuySoftwareOnline.Domain;
using BuySoftwareOnline.Web.HtmlHelpers;
using System.Web.Helpers;

namespace BuySoftwareOnline.Web.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        private ICustomerRepository cp;

        public LoginController(ICustomerRepository customerrepository)
        {
            cp = customerrepository;
        }

        public ActionResult Index()
        {
            return Redirect("/");
        }

        public PartialViewResult LoginForm()
        {
            return PartialView();
        }

        public ActionResult Update()
        {
            var customer = cp.Customers.Where(c => c.Account == Session["User"].ToString()).Select(c => c).Single();
            return View(customer);
        }

        [HttpPost]
        public ActionResult Update(Customer customer, string myCaptcha, string attempt)
        {
            try
            {
                if (CaptchaHelper.VerifyAndExpireSolution(HttpContext, myCaptcha, attempt))
                {
                    cp.SaveCustomer(customer);
                    WebMail.SmtpServer = "smtp.gmail.com";
                    WebMail.SmtpPort = 587;
                    WebMail.UserName = "phucan1108@gmail.com";
                    WebMail.Password = "11081990";
                    WebMail.From = "phucan1108@gmail.com";
                    WebMail.EnableSsl = true;
                    WebMail.Send(customer.CustomerMail, "Cập nhật thông tin thành công", "Khách hàng " + customer.CustomerName + " vừa cập nhật thông tin tài khoản thành công.");
                    TempData["Login"] = "update";
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Register");
                }
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Exit()
        {
            Session["User"] = null;
            TempData["Login"] = "exit";
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult LoginForm(FormCollection collection)
        {
            string username = collection["username"];
            string password = collection["password"];
            var customer = cp.Customers.Where(p => p.Account == username && p.Password == password).Select(p => p);
            if (customer.Count() == 0)
            {
                TempData["Login"] = "false";
                return RedirectToAction("Index");
            }
            else
            {
                Session["User"] = customer.First().Account;
                TempData["Login"] = "true";
                return RedirectToAction("Index");
            }
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(Customer customer,string myCaptcha,string attempt)
        {
            try
            {
                if (CaptchaHelper.VerifyAndExpireSolution(HttpContext, myCaptcha, attempt))
                {
                    cp.SaveCustomer(customer);
                    WebMail.SmtpServer = "smtp.gmail.com";
                    WebMail.SmtpPort = 587;
                    WebMail.UserName = "phucan1108@gmail.com";
                    WebMail.Password = "11081990";
                    WebMail.From = "phucan1108@gmail.com";
                    WebMail.EnableSsl = true;
                    WebMail.Send(customer.CustomerMail,"Chào mừng đến với website Buy Software Online","Cảm ơn bạn " + customer.CustomerName + " đã đăng ký làm khách hàng của website chúng tôi");
                    TempData["Login"] = "register";
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Register");
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
